package server.model.storeModel;

import org.apache.commons.dbutils.handlers.BeanHandler;
import server.db.DBDriver;
import server.db.DBFactory;
import server.entity.Store;
import server.entity.User;
import server.model.factory.ModelFactory;
import server.util.InfoUtils.Info;
import server.util.InfoUtils.InfoEnum;
import server.util.InfoUtils.InfoUtil;
import server.util.MyUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class StoreModel {
    public boolean setStoreId2Attr(int userId, HttpServletRequest req) {
        String sql = "select store_id from user where user_id = ? limit 1";
        User user = DBFactory.getDBDriver().query(sql, new BeanHandler<>(User.class, DBDriver.getProcess()), userId);
        if (user != null && user.getStoreId() != null) {
            req.setAttribute("storeId", user.getStoreId());
            return true;
        } else {
            return false;
        }
    }

    public Integer getStoreId(HttpServletRequest req) {
        Integer storeId = (Integer) req.getAttribute("storeId");
        if (storeId == null) {
            Integer userId = (Integer) req.getAttribute("userId");
            if (setStoreId2Attr(userId, req))
                storeId = (Integer) req.getAttribute("storeId");
        }
        return storeId;
    }


    public Info createStore(int userId, Store store) {
        Info info = null;
        String sql = "insert into store (store_name,description,store_createtime,good) values (?,?,now(),'[]')";
        List<Object> params = new ArrayList<>();

        if (!MyUtils.NOTNULL(store.getStoreName())) {
            return InfoUtil.getInfo(InfoEnum.PARAMS_NULL);
        }

        //判断店铺是否重名
        if (judgeStoreName(store.getStoreName())) {
            params.add(store.getStoreName());
        } else {
            return InfoUtil.getInfo(1, "起名真难");
        }

        //添加店铺的描述
        if (!MyUtils.NOTNULL(store.getDescription())) {
            params.add(store.getDescription());
        } else {
            params.add("描述一下你的小店吧！");
        }

        //创建店铺，插入store数据表
        if (DBFactory.getDBDriver().update(sql, params.toArray()) <= 0) {
            return InfoUtil.getInfo(InfoEnum.FAIL);
        }

        User user = new User();
        //获取store_id
        user.setStoreId(getStoreIdByName(store.getStoreName()));
        user.setUserId(userId);

        //设置user数据表中的store_id
        if (ModelFactory.getUserInfoModel().setUserInfo(user) == null) {
            return InfoUtil.getInfo(InfoEnum.FAIL);
        }
        return InfoUtil.getInfo(InfoEnum.SUCCESS);
    }

    //判断是否存在相同的店铺名
    public boolean judgeStoreName(String storeName) {
        String sql = "select * from store where store_name = ?";

        Store store = DBFactory.getDBDriver().query(sql, new BeanHandler<>(Store.class, DBDriver.getProcess()), storeName);

        if (store == null) {
            return true;
        } else return false;
    }

    //在store数据表里通过store_name查询store_id
    public Integer getStoreIdByName(String storeName) {
        String sql = "select store_id from store where store_name = ? limit 1";
        Store store = DBFactory.getDBDriver().query(sql, new BeanHandler<>(Store.class, DBDriver.getProcess()), storeName);
        return store.getStoreId();
    }
}
